package com.buba.livenuniappservernew.utils;

import cn.hutool.crypto.digest.DigestUtil;

import com.buba.livenuniappservernew.api.MemberUserManagement.entity.MemberUserManagement;
import com.buba.livenuniappservernew.common.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * token类
 */
@Service
public class TokenUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 生成token
     * @param memberUserManagement
     * @return
     */
    public String getToken(MemberUserManagement memberUserManagement){
        String tokenString = String.format("%s-%d-%d", memberUserManagement.getMemberUserRealName(), memberUserManagement.getId(), System.currentTimeMillis());
        return DigestUtil.md5Hex(tokenString);
    }

    /**
     * 获取用户id
     * @param token
     * @return
     */
    public Integer getUserIdFromToken(String token){
        String key = String.format("%s:%s", Const.TOKEN_USER, token);
        Object o = redisTemplate.opsForValue().get(key);
        if (o == null){
            return null;
        }
        return (int)o;
    }
}
